/// This Code Builds Weekly Data from master .txt files

***************************************************************************
///-----------Merge Weekly Level Files; Naming structure: Q12001 ------------/// 
local yearmin = 2001
local yearmax = 2016

insheet using "$datapath/Fund Flows/Q42000.txt", clear						// Imports first weekly file Q42000

// Time Set
split reportdate, parse(12:00:00 AM)
split assetclass, parse(-FF-)
drop reportdate2
// Date Variables																// Generate datew for xtset and datem for merge with monthly data
qui gen date = date(reportdate1,"MDY")
gen datem = mofd(date)
gen datew = wofd(date)
gen dateq = qofd(date)
format dateq %tq
format datew %tw
// Panel Data
sort fundid datew
order fundid datew
xtset fundid datew

local m = 4
tempfile weekly_fundid_`m'2000 
save `weekly_fundid_`m'2000'
***************************************************************************

***************************************************************************
insheet using "$datapath/Fund Flows/Fund/Q12001.txt", clear						// Imports Q12001 for merge with Q42000

// Time Set
split reportdate, parse(12:00:00 AM)
split assetclass, parse(-FF-)
drop reportdate2

qui gen date = date(reportdate1,"MDY")
gen datem = mofd(date)
gen datew = wofd(date)
gen dateq = qofd(date)
format dateq %tq
format datew %tw

sort fundid datew
order fundid datew
xtset fundid datew

// Merge
merge 1:1 fundid datew using `weekly_fundid_`m'2000'							// Merge 1 to 1 on fundid (fundid is unique identifier)
drop _merge
// Save
local m = 1
tempfile weekly_fundid_`m'2001 
save `weekly_fundid_`m'2001'

local mmax = 4
forvalues i = `yearmin'(1)`yearmax'{											// For years 2001 to 2016 (data available)
	if `i'==`yearmax' {																// Only 3 quarters available for 2016
		local mmax = 2
	}
	display `mmax'
	forvalues j = 1(1)`mmax'{														// For all quarters
		if `j'<4 {																		// If not last quarter, open next quarters data (`qn')
			local qn = `j'+1
			insheet using "$datapath/Fund Flows/Fund/Q`qn'`i'.txt", clear

			// Time Set
			split reportdate, parse(12:00:00 AM)
			split assetclass, parse(-FF-)
			drop reportdate2

			qui gen date = date(reportdate1,"MDY")
			gen datem = mofd(date)
			gen datew = wofd(date)
			gen dateq = qofd(date)
			format dateq %tq
			format datew %tw

			sort fundid datew

			merge 1:1 fundid datew using `weekly_fundid_`j'`i''								// Merge next quarter with previous quarter saved in tempfile `weekly_fundid_qyyyy'
			drop _merge

			// Save new merged tempfile
			tempfile weekly_fundid_`qn'`i'													// Becomes previous quarter data in next loop
			save `weekly_fundid_`qn'`i''
		}

		else if `j'==4 {																// For quarter 4 next quarters is quarter 1 of next year (`yn')
			local yn = `i'+1
			insheet using "$datapath/Fund Flows/Fund/Q1`yn'.txt", clear

			// Time Set
			split reportdate, parse(12:00:00 AM)
			split assetclass, parse(-FF-)
			drop reportdate2

			qui gen date = date(reportdate1,"MDY")
			gen datem = mofd(date)
			gen datew = wofd(date)
			gen dateq = qofd(date)
			format dateq %tq
			format datew %tw
			sort fundid datew

			merge 1:1 fundid datew using `weekly_fundid_`j'`i''
			drop _merge

			tempfile weekly_fundid_1`yn'													// After final loop, the tempfile with contain all merged quarterly data
			save `weekly_fundid_1`yn''
		}
	}
	display `i'
	display `j'
}

sort fundid datew
xtset fundid datew
save "$datapath/__EPFRdata_wflows_p.dta", replace
* end
***************************************************************************